package jtk.project4.fleet.screen.meterUpdateLog;

import java.awt.FlowLayout;
import java.util.List;

import javax.swing.SortOrder;

import jtk.project4.fleet.action.PrintAction;
import jtk.project4.fleet.domain.Equipment;
import jtk.project4.fleet.event.EquipmentEvent;
import jtk.project4.fleet.field.MeterUpdateTableField;
import nl.coderight.jazz.form.field.button.CheckButton;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;

//import jtk.project4.fleet.field.UpdateMeterSelectField;
import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;

import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class MeterUpdateView extends FormView{
	
	private CheckButton groupedButton;
	private CheckButton includeButton;
	private PushButton submitButton;
	private PushButton cancelButton;
	private PushButton printButton;
	private Action submitAction;
	private Action cancelAction;
	private Action printAction;
	
	public MeterUpdateView(){
		setSize(700, 500);
	}
	
	public void handleEvent(EquipmentEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SUBMIT:
			// check for validation errors
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				// there are validation errors so do not submit...
				errors.get(0).getField().focus();
				break;
			} else {
				// submit form and close view
				submit();
				close();
				// fall-through!
			}
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		}
	}
	
	@Override
	public void close() {
		super.close();
	}
	
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		createFields();
		createLayout();
	
	}
	
	private void createFields(){
		cancelButton = new PushButton("cancelAction");
	}
		
	private void createLayout() {
		addField(createForm());
	}
	
	private FormField createForm()
	{
		GroupControl form = new GroupControl();
		
		form.addField(createGeneralTab(), NORTH);
		form.addHorizontalSeparator();
		form.addField(createButtons(), SOUTH);
		
		return form;
	}
	
	private GroupControl createGeneralTab() {
		GroupControl form = new GroupControl();
		
		form.setLayout(new FormLayout())
		.addField(new MeterUpdateTableField("meterupdatetable"))
		;				
		return form;
	}
	
	

	private GroupControl createButtons() {
		
		GroupControl buttons = new GroupControl();
	
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));

		buttons.addField(cancelButton);
		return buttons;
	}
}
